package com.yanqu.road.server.cmd.activity.autoreward.rank;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRankModel;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class EnterNameActivityRank extends BaseChannelRank {

    public List<Integer> getActivityConditionType(){
        return null;
    }

    public EnterNameActivityRank(int activityId) {
        super(activityId);
    }

    @Override
    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg(int channelId, long serverId) {
        AutoSendRewardProto.CrossUserRankMsg.Builder msg = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        msg.setActivityId(activityId);
        EnterNameModel nameModel = ChannelEnterNameMgr.getEnterNameModel(activityId);
        if(nameModel != null){
            for (int type : getActivityConditionType()) {
                if(type == eGamePlayerEventType.EnterNameAreaFightRank.getValue() || type == eGamePlayerEventType.WaShePkGroupFightRank.getValue()){
                    List<UserEnterName> list = nameModel.getUserEnterName(channelId, serverId);
                    for (UserEnterName enterName : list) {
                        if(enterName.getServerId() != serverId){
                            continue;
                        }
                        ActivityGroupMatch groupMatch = nameModel.getActivityGroupMatch(enterName.getGroupId());
                        if(groupMatch != null){
                            EnterNameGroupRankModel groupRankModel = ChannelEnterNameRankMgr.getEnterNameGroupRankModel(activityId, type, groupMatch.getRankMatchGroupId());
                            if(groupRankModel != null){
                                int myRank = groupRankModel.getMyRank(enterName.getGroupId());
                                addUserRankData(msg, type, enterName.getUserId(), myRank);
                            }
                        }else {
                            //log
                        }
                    }
                }else {
                    Set<Integer> matchList = new HashSet<>();
                    for (Map.Entry<Integer, ActivityGroupMatch> matchEntry : nameModel.getMatchMap().entrySet()) {
                        matchList.add(matchEntry.getValue().getRankMatchGroupId());
                    }
                    for (int matchGroupId : matchList) {
                        EnterNameRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, type, matchGroupId);
                        if(rankModel != null){
                            int rank = 0;
                            for (EnterNameRank enterNameRank : rankModel.getRankList()) {
                                long userId = enterNameRank.getUserId();
                                rank++;
                                if(enterNameRank.getUserBaseInfo() != null && enterNameRank.getUserBaseInfo().getServerId() != serverId){
                                    //过滤区服
                                    continue;
                                }
                                addUserRankData(msg, type, userId, rank);
                            }
                        }
                    }
                }
            }
        }
        return msg;
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg(int channelId, long serverId) {
        return null;
    }

    @Override
    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg() {
        return null;
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg() {
        return null;
    }
}
